
import { Layers, Node, SpriteFrame, UITransform, RigidBody2D, Vec2 } from "cc";
import { LAYER_ENUM } from "../../enum";




export class Util {
    /** 
     * 安全获取数据
     */
    static getSafeData<T>(value: T, defaultData: T) {
        return value !== null && value !== undefined ? value : defaultData
    }

    /**
     * 创建UI节点
     * @param name 节点名称
     * @param layersText 节点所在的图层
     * @returns 
     */
    static CreateUINode(name: string = '', layersText: string = LAYER_ENUM.UI_2D) {
        const node = new Node(name);
        const transform = node.addComponent(UITransform);
        transform.setAnchorPoint(0, 1);
        node.layer = 1 << Layers.nameToLayer(layersText);
        return node;
    }

    /**
     * 移动节点
     * @param node 节点
     * @param radian 移动方向弧度
     * @param distance 移动距离
     */
    static MoveNode(node: Node, radian: number, distance: number) {
        const x = node.position.x + Math.cos(radian) * distance;
        const y = node.position.y + Math.sin(radian) * distance;
        node.setPosition(x, y, 0);
    }

    /**
     * 设置物理体的速度
     * @param rigidBody2D 物理体组件
     * @param radian 移动方向弧度
     * @param speed 速度大小
     */
    static SetPhysicsVelocity(rigidBody2D: RigidBody2D, radian: number, speed: number) {
        const vx = Math.cos(radian) * speed;
        const vy = Math.sin(radian) * speed;
        rigidBody2D.linearVelocity = new Vec2(vx, vy);
    }
}